package com.sghd.pojo.fight.game;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sghd.common.utils.math.RandomUtils;

import java.util.*;

/**
 * @author frank
 * @date 2019-01-15 15:32
 * @desc pvp匹配结果
 **/
public class MatchResult {

    private PlayerVo left;
    private PlayerVo right;
    private long end;
    private int scene;

    public static MatchResult valueOf(PlayerVo left, PlayerVo right, long end) {
        MatchResult result = new MatchResult();
        result.left = left;
        result.right = right;
        result.end = end;
        result.scene = RandomUtils.nextInt(10);
        return result;
    }

    public PlayerVo getLeft() {
        return left;
    }

    public void setLeft(PlayerVo left) {
        this.left = left;
    }

    public PlayerVo getRight() {
        return right;
    }

    public void setRight(PlayerVo right) {
        this.right = right;
    }

    public void ready(long player) {
        if (left.getPlayerId() == player) {
            left.setReady(true);
        } else if (right.getPlayerId() == player) {
            right.setReady(true);
        }
    }

    @JsonIgnore
    public PlayerVo getOther(long playerId) {
        if (playerId != left.getPlayerId()) {
            return left;
        }
        if (playerId != right.getPlayerId()) {
            return right;
        }
        return null;
    }

    public boolean canBegin() {
        return left.isReady() && right.isReady();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MatchResult result = (MatchResult) o;
        return Objects.equals(left.getPlayerId(), result.left.getPlayerId()) &&
                Objects.equals(right.getPlayerId(), result.right.getPlayerId());
    }

    @Override
    public int hashCode() {
        return Objects.hash(left.getPlayerId(), right.getPlayerId());
    }

    public Object toVo() {
        return MatchVo.valueOf(left, right, end, scene);
    }

    public Collection<Long> getPlayerIds() {
        return Arrays.asList(left.getPlayerId(), right.getPlayerId());
    }

    public static class MatchVo {
        private Map<String, Object> left;
        private Map<String, Object> right;
        private int scene;
        private long end;

        public static MatchVo valueOf(PlayerVo left, PlayerVo right, long end, int scene) {
            MatchVo matchVo = new MatchVo();
            matchVo.left = new HashMap<>();
            matchVo.left.put("id", left.getPlayerId());
            matchVo.left.put("name", left.getName());
            matchVo.left.put("headIcon", left.getHeadIcon());

            matchVo.right = new HashMap<>();
            matchVo.right.put("id", right.getPlayerId());
            matchVo.right.put("name", right.getName());
            matchVo.right.put("headIcon", right.getHeadIcon());
            matchVo.end = end;
            matchVo.scene = scene;
            return matchVo;
        }

        public Map<String, Object> getLeft() {
            return left;
        }

        public void setLeft(Map<String, Object> left) {
            this.left = left;
        }

        public Map<String, Object> getRight() {
            return right;
        }

        public void setRight(Map<String, Object> right) {
            this.right = right;
        }

        public long getEnd() {
            return end;
        }

        public void setEnd(long end) {
            this.end = end;
        }

        public int getScene() {
            return scene;
        }

        public void setScene(int scene) {
            this.scene = scene;
        }
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    public int getScene() {
        return scene;
    }

    public void setScene(int scene) {
        this.scene = scene;
    }
}
